import json
import datetime

from flask import Blueprint
from flask import request
from flask import Response

from sqlalchemy import Column, String, Integer
from flask_sqlalchemy import SQLAlchemy

from app.comm.db import db
from app.system import auth
from app.comm.db import jsonformater
from app.comm.pub import RestEntity
from app.comm.pub import class2dict

appmenuservice = Blueprint('appmenu', __name__, )


class AppMenu(db.Model):
    __tablename__ = 'app_menu'
    user_id = Column(Integer, primary_key=True)
    user_name = Column(String(80), unique=True, nullable=False)
    email = Column(String(120), unique=True, nullable=False)

    def as_dict(self):
        return {c.name: str(getattr(self, c.name)) for c in self.__table__.columns}

    def __repr__(self):
        return '<User %r>' % self.username

# return json.dumps(dict1)
@appmenuservice.route("/rest/app/menu/list", methods=['POST', 'GET'])
def appMenuList():
    appid = request.values.get('appid')
    # dict1 = json.loads(a)
    tables = db.session.execute(
        "SELECT  m.app_id, a.*,m.order_num from app_menu_app m LEFT JOIN app_menu a on m.menu_id=a.menu_id where a.visible=1 and m.app_id='"+appid+"'").fetchall()
    datas = [dict(r) for r in tables]
    result = RestEntity(0, 0, datas, "登陆成功！")
    return json.dumps(class2dict(result), default=jsonformater)

    # return json.dumps(dict1)